package com.game.serivce;

import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import net.SocketEnum;

import org.apache.log4j.Logger;

import com.dictionary.DictionaryTabel;


/**
 * 广播服务
 * @author wuxiaojian
 *
 */
public class GuangboService {
	private static Logger logger = Logger.getLogger(GuangboService.class.getName());
	private GameRoomService gameRoomService;
	
	public GuangboService(GameRoomService gameRoomService){
		this.gameRoomService = gameRoomService;
		setTimerTask();
	}
	
	private ScheduledExecutorService scheduExec = Executors.newSingleThreadScheduledExecutor();
	private void setTimerTask() {
		Runnable task = new Runnable() {
			public void run() {
				try {
					sendAllRoomMessage();
				} catch (Exception e) {
					e.printStackTrace();
					logger.error(e.getMessage(), e);
				}
			}
		};
		scheduExec.scheduleWithFixedDelay(task, 0, 5000, TimeUnit.MILLISECONDS);
	}
	private ArrayList<String> guangboStrList = new ArrayList<String>();
	private void sendAllRoomMessage(){
		if(guangboStrList.size() > 0){
			String guangboStr = guangboStrList.remove(0);
			for (int i = 0; i < gameRoomService.roomDeskServices.length; i++) {
				for (int j = 0; j < gameRoomService.roomDeskServices[i].deskPlayerServices.length; j++) {
					DeskPlayerService deskPlayerService = gameRoomService.roomDeskServices[i].deskPlayerServices[j];
					if (deskPlayerService != null && deskPlayerService.playerService.deskPlayer.getPlayerType() == DictionaryTabel.PlayerType_People)
						deskPlayerService.playerService.sendMessage(SocketEnum.guangboI, guangboStr);
				}
			}
		}
	}
	
	public void addGuangboStr(String guangboStr){
		guangboStrList.add(guangboStr);
	}
}
